package dto;

// Generated May 6, 2011 4:19:20 PM by Hibernate Tools 3.3.0.GA

import java.util.Iterator;
import org.hibernate.Query;
import java.util.Vector;
import org.hibernate.criterion.MatchMode;
import SessionFactory.SessionFactoryUtil;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.SessionFactory;
import static org.hibernate.criterion.Example.create;

/**
 * Home object for domain model class Announcement.
 * @see dto.Announcement
 * @author Hibernate Tools
 */
public class AnnouncementHome 
{ 
	private static final Log log = LogFactory.getLog(AnnouncementHome.class);
	private final SessionFactory sessionFactory =  SessionFactoryUtil.getInstance();
         //private Session mSession;
         //private Transaction mTransaction;
	public void persist(Announcement transientInstance) {
		log.debug("persisting Announcement instance");
		try 
                {
			sessionFactory.getCurrentSession().persist(transientInstance);
			log.debug("persist successful");
		} 
                catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void attachDirty(Announcement instance) {
		log.debug("attaching dirty Announcement instance");
		try {
			sessionFactory.getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Announcement instance) {
		log.debug("attaching clean Announcement instance");
		try {
			sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void delete(Announcement persistentInstance) {
		log.debug("deleting Announcement instance");
		try {
			sessionFactory.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Announcement merge(Announcement detachedInstance) {
		log.debug("merging Announcement instance");
		try {
			Announcement result = (Announcement) sessionFactory
					.getCurrentSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public Announcement findById(Integer id) {
		log.debug("getting Announcement instance with id: " + id);
		try {
			Announcement instance = (Announcement) sessionFactory
					.getCurrentSession().get("dto.Announcement", id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Announcement> findByExample(Announcement instance) {
		log.debug("finding Announcement instance by example");
		try {
			List<Announcement> results = (List<Announcement>) sessionFactory
					.getCurrentSession().createCriteria("dto.Announcement")
					.add(create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
         public Vector<Announcement> getAllAnnouncements()
        {
            Vector<Announcement> vect = new Vector<Announcement>();
            Announcement ann;
            Query q = sessionFactory.getCurrentSession().createQuery("from Announcement");
            Iterator  it = q.list().iterator();
            while(it.hasNext())
            {
            ann  = new Announcement();
            ann  = (Announcement)it.next();
            vect.add(ann);
        }

        return vect;
        }

    public List getannouncement() {
        System.out.println("I am inside getannouncement");
        Announcement ann=new Announcement();
        org.hibernate.criterion.Example exampleUser =org.hibernate.criterion.Example.create(ann).ignoreCase().enableLike(MatchMode.ANYWHERE);
        List results = sessionFactory.getCurrentSession().createCriteria("dto.Announcement").add(exampleUser).list();
         System.out.println("find by example successful, result size: " + results.size());       
        return results;

    }
}
